package com.shine.app.ui;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shine.app.AppManager;
import com.shine.app.BaseActivity;
import com.shine.app.R;
import com.shine.app.bean.MyReviewBean;
import com.shine.app.bean.responses.ValidateResponse;
import com.shine.app.inject.InjectView;
import com.shine.app.inject.Injector;

public class ViewMyInfo_ReviewDeleteDialogActivity extends BaseActivity
		implements OnClickListener {

	@InjectView(R.id.submit)
	private TextView submiTextView;
	@InjectView(R.id.cancel)
	private TextView cancelTextView;
	@InjectView(R.id.message)
	private TextView messageTextView;
	@InjectView(R.id.layout)
	private LinearLayout layout;
	@InjectView(R.id.progressBar)
	private ProgressBar progressBar;
	private MyReviewBean myReviewBean;
	private CourseReviewDeleteAsyncTask courseReviewDeleteAsyncTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.dialog_cancelteetimes);
		Injector.get(this).inject();

		init();
	}

	private void init() {
		progressBar.setVisibility(View.GONE);
		myReviewBean = (MyReviewBean) getIntent().getSerializableExtra(
				"myReviewBean");
		cancelTextView.setText(R.string.cancel);
		submiTextView.setText(R.string.delete);
		messageTextView.setText(R.string.review_deletedialog);
		submiTextView.setOnClickListener(this);
		cancelTextView.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.cancel:
			AppManager.getAppManager().finishActivity();
			break;

		case R.id.submit:
			layout.setVisibility(View.GONE);
			String userId = appContext.getLoginInfo().getUser().getUserId();
			courseReviewDeleteAsyncTask = new CourseReviewDeleteAsyncTask();
			courseReviewDeleteAsyncTask.execute(myReviewBean.getId(), userId,
					myReviewBean.getCourseId());
			break;

		default:
			break;
		}
	}

	public class CourseReviewDeleteAsyncTask extends
			AsyncTask<Object, Integer, ValidateResponse> {
		private int courseReviewId;
		private String userId;
		private int courseId;

		@Override
		protected ValidateResponse doInBackground(Object... params) {
			ValidateResponse response = null;
			courseReviewId = (Integer) params[0];
			userId = (String) params[1];
			courseId = (Integer) params[2];
			response = appContext.deleteCourseReview(courseReviewId, userId,
					courseId);
			return response;
		}

		@Override
		protected void onPostExecute(ValidateResponse result) {
			super.onPostExecute(result);
			if (result != null && result.isData()) {
				AppManager.getAppManager().finishActivity(
						ViewMyInfoReview_detailActivity.class);
			}
			AppManager.getAppManager().finishActivity();
			progressBar.setVisibility(View.GONE);
			overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onDestroy() {
		if (courseReviewDeleteAsyncTask != null
				&& courseReviewDeleteAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			courseReviewDeleteAsyncTask.cancel(true);
		}
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AppManager.getAppManager().finishActivity();
		}
		return true;
	}
}
